package com.app.backend.activiti.controller;

import cn.hutool.core.util.StrUtil;
import com.app.backend.activiti.entity.ActBiz;
import com.app.backend.activiti.entity.ActProcess;
import com.app.backend.activiti.service.ActBusinessService;
import com.app.backend.activiti.service.ActProcessService;
import com.app.backend.activiti.service.mybatis.IActService;
import com.app.backend.common.constant.ActConstant;
import com.app.backend.common.response.BaseResponse;
import com.app.backend.common.response.ResponseCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.activiti.engine.RuntimeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * @program: ActBizController
 * @description:
 * @author: Zhun.Xiao
 * @create: 2020-02-27 10:47
 **/


@Slf4j
@RestController
@Api("业务申请管理")
@RequestMapping("/api/v1/act/biz")
public class ActBizController {

    @Autowired
    private IActService iActService;

    @Autowired
    RuntimeService runtimeService;

    @Autowired
    ActBusinessService actBusinessService;

    @Autowired
    ActProcessService actProcessService;

    @PostMapping(value = "/apply")
    @ApiOperation(value = "提交申请 启动流程")
    public BaseResponse apply(ActBiz act) {
        ActBiz actBusiness = actBusinessService.get(act.getId());
        if (actBusiness == null) {
            return new BaseResponse(ResponseCode.PARAM_FAIL, "actBiz表中该id不存在");
        }
        act.setTableId(actBusiness.getTableId());

        String processInstanceId = actProcessService.startProcess(act);
        actBusiness.setProcInstId(processInstanceId);
        actBusiness.setStatus(ActConstant.STATUS_TO_PROCESS);
        actBusiness.setResult(ActConstant.RESULT_PROCESSING);
        actBusiness.setApplyTime(new Date());
        actBusinessService.update(actBusiness);
        return new BaseResponse();
    }


    @PostMapping("/cancel")
    @ApiOperation("撤回申请")
    public BaseResponse cancel(@RequestParam String id,
                               @RequestParam String processIncId,
                               @RequestParam(required = false) String reason) {
        if (StrUtil.isBlank(reason)) {
            reason = "";
        }
        runtimeService.deleteProcessInstance(processIncId, "cancel:" + reason);
        ActBiz actBiz = actBusinessService.get(id);
        actBiz.setStatus(ActConstant.STATUS_CANCELED);
        actBiz.setResult(ActConstant.RESULT_TO_SUBMIT);
        actBusinessService.update(actBiz);
        return new BaseResponse();
    }


    @RequestMapping(value = "/delByIds/{ids}", method = RequestMethod.DELETE)
    @ApiOperation(value = "通过id删除草稿状态申请")
    public BaseResponse delByIds(@PathVariable String[] ids) {

        for (String id : ids) {
            ActBiz actBusiness = actBusinessService.get(id);
            if (!ActConstant.STATUS_TO_APPLY.equals(actBusiness.getStatus())) {
                return new BaseResponse(ResponseCode.PARAM_FAIL, "删除失败, 仅能删除草稿状态的申请");
            }
            // 删除关联业务表
            ActProcess actProcess = actProcessService.get(actBusiness.getProcDefId());
            iActService.deleteBusiness(actProcess.getBusinessTable(), actBusiness.getTableId());
            actBusinessService.deleteById(id);
        }
        return new BaseResponse();
    }


}